package model;

import java.util.ArrayList;
import java.util.HashMap;

public class CustomMap {

	protected HashMap<String, Integer> map;
	protected int maxValue;
	protected int initValue;
	protected ArrayList<String> keys;

	public CustomMap() {
		map = new HashMap<String, Integer>();
	}

	public CustomMap(int initValue, int maxValue, ArrayList<String> keys) {
		map = new HashMap<String, Integer>();
		this.keys = keys;
		this.initValue = initValue;
		this.maxValue = maxValue;
		init();
	}

	protected void init() {
		for (String key : keys) {
			map.put(key, initValue);
		}
	}

	public void add(String name, int value) {
		int newValue = value + map.get(name);
		map.put(name, newValue < maxValue ? newValue : maxValue);
	}

	public void remove(String key, int value) {
		int newValue = map.get(key) - value;
		map.put(key, newValue > 0 ? newValue : 0);
	}

	public void add(HashMap<String, Integer> newMap) {
		for (String key : newMap.keySet()) {
			int value = newMap.get(key) + map.get(key);
			map.put(key, value < maxValue ? value : maxValue);
		}
	}

	public void remove(HashMap<String, Integer> newMap) {
		for (String key : newMap.keySet()) {
			int value = map.get(key) - newMap.get(key);
			map.put(key, value > 0 ? value : 0);
		}
	}

	public boolean contains(HashMap<String, Integer> newMap) {
		for (String key : newMap.keySet()) {
			if (map.get(key) < newMap.get(key)) {
				return false;
			}
		}
		return true;
	}
	public boolean contains(String key, int value) {
		if (map.get(key) >= value) {
			return true;
		}
		return false;
	}

	public boolean isFull() {
		for (String key : map.keySet()) {
			if (map.get(key) < maxValue) {
				return false;
			}
		}
		return true;
	}
	public boolean isEmpty() {
		for (String key : map.keySet()) {
			if (map.get(key) > 0) {
				return false;
			}
		}
		return true;
	}

	// GETTER AND SETTER
	public int getCountOf(String key) {
		return map.get(key);
	}
	public void setCountOf(String name, int value) {
		map.put(name, value);
	}

	public HashMap<String, Integer> getMap() {
		return map;
	}

	public int getMaxValue() {
		return maxValue;
	}
}